<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <!-- <el-button type="primary" icon="el-icon-plus" @click="add" ></el-button> -->
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'repeal.confirm'" >批量删除</el-button>
                <el-input v-model="bar_code" ref="barCodeInput" placeholder="放入条码" style="width: 200px;margin-left: 10px" @keyup.enter="getTable()"></el-input>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <!-- <el-input v-model="search.search_keyword"  class="input-with-search" style="width: 300px" @keyup.enter="upsearch">
                        <template #prepend>
                            <el-select v-model="search.search_field" filterable clearable placeholder="请选择" style="width: 110px">
                                <el-option label="作废单号" value="purchase_ware_code"/>
                                <el-option label="品名" value="goods_name"/>
                            </el-select>
                        </template>
                    </el-input> -->
                    <el-input v-model="search.keyword" placeholder="条码" @keyup.enter="upsearch"></el-input>
                    <el-input v-model="search.goods_name" placeholder="品名" @keyup.enter="upsearch"></el-input>
                    <el-select v-model="search.status" placeholder="审核状态" clearable>
                        <el-option label="待审核" :value="0"></el-option>
                        <el-option label="通过" :value="1"></el-option>
                        <el-option label="退回" :value="2"></el-option>
                    </el-select>
                    <el-date-picker v-model="search.action_date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" :params="{is_delete: 1}" border>
                <el-table-column type="selection" width="50" align="center"></el-table-column>
                <!-- <el-table-column type="index" width="50" align="center"></el-table-column> -->
                <el-table-column label="类型" prop="reject_type" width="120" align="center">
                    <template #default="{row}">
                        {{row.reject_type == 1 ? '码数' : row.reject_type == 2 ? '米数' : '重量+米数'}}
                    </template>
                </el-table-column>
                <el-table-column label="消耗" prop="use_num" width="100" align="center"></el-table-column>
                <el-table-column label="布卷条码" prop="roll_code" width="130" align="center"></el-table-column>
                <el-table-column label="日期" prop="action_date" width="120" align="center"></el-table-column>
                <el-table-column label="入库单号" prop="purchaseware.purchase_ware_code" width="150" align="center"></el-table-column>
                <el-table-column label="检验单号" prop="test.test_code" width="150" align="center"></el-table-column>
                <el-table-column label="货号" prop="goods_code" width="120" align="center"></el-table-column>
                <el-table-column label="品名" prop="goods_name" width="120" align="center"></el-table-column>
                <el-table-column label="来数" prop="come" width="100" align="center"></el-table-column>
                <el-table-column label="净重" prop="net_weight" width="100" align="center"></el-table-column>
                <el-table-column label="米数" prop="meter" width="100" align="center"></el-table-column>
                <el-table-column label="码数" prop="code_number" width="100" align="center"></el-table-column>
                <el-table-column label="剪数" prop="cut" width="100" align="center"></el-table-column>
                <el-table-column label="折码" prop="folding.name" width="100" align="center"></el-table-column>
                <el-table-column label="克重" prop="gram_weight" width="100" align="center"></el-table-column>
                <el-table-column label="计重" prop="weight" width="100" align="center"></el-table-column>
                <el-table-column label="计数" prop="number" width="100" align="center"></el-table-column>
                <el-table-column label="颜色" prop="color" width="120" align="center"></el-table-column>
                <el-table-column label="Color" prop="colorName.name" width="120" align="center"></el-table-column>
                <el-table-column label="花型" prop="flower" width="120" align="center"></el-table-column>
                <el-table-column label="次品种类" prop="rejectCate.name" width="120" align="center"></el-table-column>
                <el-table-column label="机台" prop="machine.name" width="120" align="center"></el-table-column>
                <el-table-column label="检验员" prop="work_uids_name" width="120" align="center"></el-table-column>
                <el-table-column label="操作" prop="action_type" width="120" align="center">
                    <template #default="{row}">
                        {{row.action_type == 1 ? '最终检验' : '跑米'}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="200">
                    <template #default="scope">
                        <div class="buttonFlex">
                            <el-popconfirm title="确定撤销作废吗？" @confirm="table_revoke(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button type="warning" size="small" v-auth="'repeal.revoke'">撤销作废</el-button>
                                </template>
                            </el-popconfirm>
                            <el-popconfirm title="确定彻底删除吗？" @confirm="table_confirm_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button type="danger" size="small" v-auth="'repeal.confirm'">彻底删除</el-button>
                                </template>
                            </el-popconfirm>
                        </div>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false" draggable></check-status>
    <upload-dialog v-if="dialog.upload" ref="uploadDialog" @success="handleSuccess" @closed="dialog.upload = false"></upload-dialog>
    
</template>

<script>
import saveDialog from './save'

import CheckStatus from '../../checkStatus.vue'
import uploadDialog from '../../upload.vue'
import config from '@/config'
export default {
    name: '采购计划管理',
    components: {
        saveDialog, CheckStatus, uploadDialog
    },
    data() {
        return {
            dialog: {
                save: false,
                check: false,
                upload: false,
            },
            apiObj: this.$API.business.inspect.execute.list,
            selection: [],
            search: {
                purchase_code: null,
                action_date: [this.moment().subtract(30, 'days').format('YYYY-MM-DD'), this.moment().format('YYYY-MM-DD')],
                status: null,
                goods_name: null
            },
            bar_code: null,
        }
    },
    mounted() {
    },
    methods: {
        //撤销作废
        async table_revoke(row) {
            var reqData = { id: row.id,back_delete: 1 }
            var res = await this.$API.business.inspect.execute.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("操作成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        //彻底删除
        async table_confirm_del(row) {
            var reqData = { id: row.id, comform_delete: 1}
            var res = await this.$API.business.inspect.execute.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess(data, mode) {
            if (mode == 'add') {
                this.$refs.table.refresh()
            } else if (mode == 'edit') {
                this.$refs.table.refresh()
            }
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.search.action_date = [this.moment().subtract(30, 'days').format('YYYY-MM-DD'), this.moment().format('YYYY-MM-DD')],
            this.$refs.table.upData(this.search)
        },
        //批量删除
        async batch_del() {
            var delete_ids = [];
            this.selection.forEach((item) => {
                delete_ids.push(item.id)
            })

            this.$confirm(`确定彻底删除选中的 ${this.selection.length} 项吗？`, '提示', {
                type: 'warning'
            }).then( async() => {
                var reqData = { id: delete_ids, comform_delete: 1 }
                var res = await this.$API.business.inspect.execute.delete.post(reqData);
                if (res.code == 1) {
                    this.$refs.table.refresh()
                    this.$message.success("删除成功")
                } else {
                    this.$alert(res.message, "提示", { type: 'error' })
                }
            }).catch(() => {

            })
        },

        // 扫码
        async getTable(){
            if(!this.bar_code) return
            var res = await this.$API.ware.ware.repeal_scan.get({roll_code: this.bar_code})
            if(res.code == 1){
                // 创建新的Audio实例播放成功声音，避免连续播放时相互干扰
                const successAudio = new Audio(`${config.API_URL}//audio/ok.wav`);
                successAudio.play();
                this.upsearch()
                this.bar_code = ""
            }else{
                const errorAudio = new Audio(`${config.API_URL}/audio/error.wav`);
                errorAudio.play();
                this.$message.error(res.message)
                this.$refs.barCodeInput.select()
            }
        },
    }
}
</script>

<style></style>
